<?php

namespace App\Controllers\Lessions;

use App\Models\Lession\CommentCopy;
use App\Models\Lession\Scolumn;
use App\Models\Lession\Lession;
/**
    提交评论：
 * 1：对课程评论，  //lession_id
 * 2：对一级评论回复 //lession_id,comment_id,
 * 3：对一级一下的’对说‘评论回复;
 */
class CommitComment extends \App\Libs\Base\BaseAction
{

    public function execute()
    {
        if(!$this->request->hasPost('lession_id') || !$this->request->hasPost('content')){
            return \App\Libs\Views\Json::view(1, '对哪节课评论?', []);
        }
        $lessionID = $this->request->getPost('lession_id');
        $content = $this->request->getPost('content');
        if(empty(trim($content))){
            return \App\Libs\Views\Json::view(1, '请输入内容', []);
        }
        if($this->request->hasPost('comment_id')){
            if(!$this->request->hasPost('to_uid')){
                return \App\Libs\Views\Json::view(1, '要回复谁呢？', []);
            }
            $commentID = $this->request->getPost('comment_id');
            $toUID = $this->request->getPost('to_uid');
            $comment = new CommentCopy();
            //$comment->from_uid= $this->session->user->id;
            $comment->from_uid = 1;
            $comment->to_uid = $toUID;
            $comment->lession_id = $lessionID;
            $comment->content = $content;
            $comment->fid = $commentID;
            $comment->ctime = date('Y:m:d H:i:s');
        }else{
            //没有课程id那么就是顶级评论;
            $comment = new CommentCopy();
            //$comment->from_uid= $this->session->user->id;
            $comment->from_uid= 1;
            $comment->to_uid = 0;
            $comment->lession_id = $lessionID;
            $comment->content =$content;
            $comment->fid = 0;
            $comment->ctime = date('Y:m:d H:i:s');
        }
        if($comment->save()){
            //增加课程的评论数；
            $matchLession = Lession::findOne('id='.$lessionID);
            $matchLession->comment_num +=1;
            $matchLession->update();
            //增加专栏的评论数；
            $course = $matchLession->getCourseData();
            $course->comment_num+=1;
            $course->update();
            return \App\Libs\Views\Json::view(0, '评论成功', []);
        }else{
            return \App\Libs\Views\Json::view(1, '失败，系统繁忙;', []);
        }


    }
}
